#!/usr/bin/env node

const _ = require('lodash');
const parser = require('node-schedule/node_modules/cron-parser');
const moment = require('moment');

function showHelp() {
    const list = [];
    list.push(`*    *    *    *    *    *`);
    list.push(`┬    ┬    ┬    ┬    ┬    ┬`);
    list.push(`│    │    │    │    │    │`);
    list.push(`│    │    │    │    │    └ day of week(0 - 7)(0 or 7 is Sun)`);
    list.push(`│    │    │    │    └───── month(1 - 12)`);
    list.push(`│    │    │    └────────── day of month(1 - 31)`);
    list.push(`│    │    └─────────────── hour(0 - 23)`);
    list.push(`│    └──────────────────── minute(0 - 59)`);
    list.push(`└───────────────────────── second(0 - 59, OPTIONAL)`);
    console.log(list.join('\n'));
    console.log();
    console.log('Usage:');
    console.log('   xtask "0 * * * * * " [n]');
}

function test(rule, count = 10) {
    const interval = parser.parseExpression(rule || '0 0/10 * * * *');
    for (let i = 0; i < count; ++i) {
        console.log(moment(interval.next()._date.ts).format('YYYY-MM-DD HH:mm:ss'));
    }
}

const list = process.argv.slice(2);
if (list[0] === '-h' || !list[0]) {
    showHelp();
} else {
    const _list = (list[0] || '').split(/\s/).filter(Boolean);
    for (let i=_list.length; i<6; i++) {
        _list[i] = '*';
    }
    test(_list.join(' '), list[1]);
}